package com.c.yinyuezhushou.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.c.yinyuezhushou.Dialog.HomeDialog;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.Service.MusicService;

import java.util.List;

public class MusicRecAdapter extends RecyclerView.Adapter{
    private List list;
    private MusicService iBinder;
    private FragmentManager fragmentManager;
    private MyInterface myInterface;

    public MusicRecAdapter(List list, MusicService iBinder, FragmentManager fragmentManager,MyInterface myInterface) {
        this.list = list;
        this.iBinder = iBinder;
        this.fragmentManager = fragmentManager;
        this.myInterface = myInterface;
    }


    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView Song;
        TextView Singer;
        CardView cardView;
        ImageView imageView1;
        RelativeLayout relativeLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.music_rec_image);
            Song = itemView.findViewById(R.id.music_rec_text1);
            Singer = itemView.findViewById(R.id.music_rec_text2);
            cardView = itemView.findViewById(R.id.music_rec_card);
            imageView1 = itemView.findViewById(R.id.music_rec_image2);
            relativeLayout = itemView.findViewById(R.id.music_rec_re);
        }
    }

    static class Load extends RecyclerView.ViewHolder{
        TextView textView;
        public Load(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.music_rec_2_text);
        }
    }

    @Override
    public int getItemViewType(int position) {
            if (list.get(position) instanceof String){
                return 1;
            }
        return 0;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==1){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music_recycler2,parent,false);
            return new Load(view);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music_recycler,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Load){
            String loadtype = (String) list.get(position);
            Load load = (Load) holder;
            switch (loadtype){
                case "gone":
                    load.textView.setText("到底了......");
                break;
            }
        }else {
            MusicItem musicItem= (MusicItem) list.get(position);
            ViewHolder viewHolder = (ViewHolder) holder;
            Glide.with(viewHolder.imageView.getContext()).load(musicItem.getImageurl()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.imageView);
            viewHolder.Singer.setText(musicItem.getSinger());
            viewHolder.Song.setText(musicItem.getSong());

            viewHolder.imageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    HomeDialog dialog = new HomeDialog(musicItem,list);
                    dialog.show(fragmentManager,null);
                }
            });

            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        iBinder.prepareToPlay(musicItem);

                        iBinder.setMusicItems(list);

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            };

            View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    if (myInterface!=null){
                        myInterface.musicItem((MusicItem) list.get(position));
                        return true;
                    }
                    return false;
                }
            };


            viewHolder.cardView.setOnClickListener(onClickListener);

            viewHolder.cardView.setOnLongClickListener(onLongClickListener);

        }
    }




    @Override
    public int getItemCount() {
        return list.size();
    }

    public void addData(List list){
        this.list=list;
        //notifyItemChanged();
        this.notifyDataSetChanged();
    }
    public void addLoad(String l){
        if (!(list.get(list.size()-1) instanceof String)){
        list.add(l);
        this.notifyDataSetChanged();
        }
    }

}
